package com.platform.jpa.utils;

import java.util.Random;
import java.util.regex.Pattern;

public class StringUtil {
    public static boolean isEmpty(Object obj) {
        return null == obj || obj.toString().trim().isEmpty();
    }

    public static boolean isEmpty(String str) {
        return null == str || str.trim().isEmpty() || "".equals(str);
    }

    /**
     * 判断字符串中是否包含中文
     *
     * @param str 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        return p.matcher(str).find();
    }

    public static String join(Object[] a, String separator) {
        if (a == null || a.length == 0) {
            return "";
        }
        if (a.length == 1) {
            return a[0] + "";
        }
        String str = a[0] + "";
        for (int i = 1; i < a.length; i++) {
            str += separator + a[i];
        }
        return str;
    }

    /**
     * 英文+数字随机字符串
     *
     * @param length
     * @return
     */
    public static String genRandomCode(int length) {
        int maxNum = 62;
        int i;
        int count = 0;
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
                'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < length) {
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        return pwd.toString();
    }
}
